package com.int_tech.api.service;

import com.int_tech.api.etity.*;
import com.int_tech.car.dao.CarDao;
import com.int_tech.car.entity.Car;
import com.int_tech.control.dao.SocketLogSendDao;
import com.int_tech.control.entity.SocketLogSend;
import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.station.dao.StationDao;
import com.int_tech.station.entity.Station;
import com.int_tech.utils.CoreDateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ApiService {

    @Autowired
    private CarDao carDao;

    @Autowired
    private MessionDao messionDao;

    @Autowired
    private StationDao stationDao;

    @Autowired
    private SocketLogSendDao socketLogSendDao;


    /**
     * 汽车:条件查询时动态组装条件+分页
     */
    public Page<Car> getCarAndDynamic(CarSearchUtil carSearchUtil, PageUtil pageUtil) {
        Pageable pageable=new PageRequest(pageUtil.getPageNumber()-1, pageUtil.getPageSize());  //分页信息
        //动态条件
        Specification<Car> spec = new Specification<Car>(){
            @Override
            public Predicate toPredicate(Root<Car> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();

                //根据ID查询
                if (carSearchUtil.getId()!=null) {
                    list.add(cb.equal(root.get("id").as(Integer.class), carSearchUtil.getId()));
                }

                //模糊查询车牌号
                if (StringUtils.isNotEmpty(carSearchUtil.getCarNumber())) {
                    list.add(cb.like(root.get("carNumber").as(String.class), "%" + carSearchUtil.getCarNumber() + "%"));
                }

                //模糊查询中文车牌号
                if (StringUtils.isNotEmpty(carSearchUtil.getCarCnumber())) {
                    list.add(cb.like(root.get("carCnumber").as(String.class), "%" + carSearchUtil.getCarCnumber() + "%"));
                }

                //根据站点ID查询
                if (carSearchUtil.getStationId()!=null) {
                    list.add(cb.equal(root.get("stationId").as(Integer.class), carSearchUtil.getStationId()));
                }

                //模糊查询所属站点
                if (StringUtils.isNotEmpty(carSearchUtil.getStationName())) {
                    list.add(cb.like(root.get("stationName").as(String.class), "%" + carSearchUtil.getStationName() + "%"));
                }

                //模糊查询车辆品牌
                if (StringUtils.isNotEmpty(carSearchUtil.getCarBrand())) {
                    list.add(cb.like(root.get("carBrand").as(String.class), "%" + carSearchUtil.getCarBrand() + "%"));
                }
                list.add(cb.equal(root.get("isDel").as(int.class), 0));
                //排序
                query.orderBy(cb.asc(root.get("id").as(Integer.class)));
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }

        };
        return carDao.findAll(spec, pageable);
    }

    /**
     * 电子锁任务:条件查询时动态组装条件+分页
     * @param messionSearchUtil
     * @return
     */
    public Page<Mession> getMessionAndDynamic(MessionSearchUtil messionSearchUtil, PageUtil pageUtil) {
        Pageable pageable=new PageRequest(pageUtil.getPageNumber()-1, pageUtil.getPageSize());  //分页信息
        //动态条件
        Specification<Mession> spec = new Specification<Mession>(){
            @Override
            public Predicate toPredicate(Root<Mession> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();

                //根据ID查询
                if (messionSearchUtil.getId()!=null) {
                    list.add(cb.equal(root.get("id").as(Integer.class), messionSearchUtil.getId()));
                }

                //模糊查询任务编号
                if (StringUtils.isNotEmpty(messionSearchUtil.getMessionNumber())) {
                    list.add(cb.like(root.get("messionNumber").as(String.class), "%" + messionSearchUtil.getMessionNumber() + "%"));
                }

                //模糊查询车辆编号
                if (StringUtils.isNotEmpty(messionSearchUtil.getCarNumber())) {
                    list.add(cb.like(root.get("carNumber").as(String.class), "%" + messionSearchUtil.getCarNumber() + "%"));
                }

                //是否完成
                if (messionSearchUtil.getIsOver()!=null) {
                    list.add(cb.equal(root.get("isOver").as(Integer.class), messionSearchUtil.getIsOver()));
                }

                //模糊查询始发地
                if (StringUtils.isNotEmpty(messionSearchUtil.getStartAddress())) {
                    list.add(cb.like(root.get("startAddress").as(String.class), "%" + messionSearchUtil.getStartAddress() + "%"));
                }

                //模糊查询目的地
                if (StringUtils.isNotEmpty(messionSearchUtil.getEndAddress())) {
                    list.add(cb.like(root.get("endAddress").as(String.class), "%" + messionSearchUtil.getEndAddress() + "%"));
                }

                //是否同步了时间状态数据
                if (messionSearchUtil.getIsAnsyCarTime()!=null) {
                    list.add(cb.equal(root.get("isAnsyCarTime").as(Integer.class), messionSearchUtil.getIsAnsyCarTime()));
                }

                //异常任务
                if (messionSearchUtil.getYiChang()!=null) {
                    list.add(cb.equal(root.get("yiChang").as(Integer.class), messionSearchUtil.getYiChang()));
                }

                //1，代表以获取终点位置;2,为已更新起点坐标;3,代表任务路线绘制完成
                if (messionSearchUtil.getCheckStatus()!=null) {
                    list.add(cb.equal(root.get("checkStatus").as(Integer.class), messionSearchUtil.getCheckStatus()));
                }

                //排序
                query.orderBy(cb.asc(root.get("id").as(Integer.class)));
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }

        };
        return messionDao.findAll(spec, pageable);
    }

    /**
     * 场站:条件查询时动态组装条件+分页
     * @param stationSearchUtil
     * @return
     */
    public Page<Station> getStationAndDynamic(StationSearchUtil stationSearchUtil, PageUtil pageUtil) {
        Pageable pageable=new PageRequest(pageUtil.getPageNumber()-1, pageUtil.getPageSize());  //分页信息
        //动态条件
        Specification<Station> spec = new Specification<Station>(){
            @Override
            public Predicate toPredicate(Root<Station> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();

                //根据ID查询
                if (stationSearchUtil.getId()!=null) {
                    list.add(cb.equal(root.get("id").as(Integer.class), stationSearchUtil.getId()));
                }

                //根据场站编码查询
                if (stationSearchUtil.getStationCode()!=null) {
                    list.add(cb.equal(root.get("stationCode").as(Integer.class), stationSearchUtil.getStationCode()));
                }

                //模糊查询场站名称
                if (StringUtils.isNotEmpty(stationSearchUtil.getName())) {
                    list.add(cb.like(root.get("name").as(String.class), "%" + stationSearchUtil.getName() + "%"));
                }

                //根据客户编号查询
                if (stationSearchUtil.getClientId()!=null) {
                    list.add(cb.equal(root.get("clientId").as(Integer.class), stationSearchUtil.getClientId()));
                }

                //模糊查询客户名称
                if (StringUtils.isNotEmpty(stationSearchUtil.getClentName())) {
                    list.add(cb.like(root.get("clentName").as(String.class), "%" + stationSearchUtil.getClentName() + "%"));
                }

                list.add(cb.equal(root.get("isDel").as(int.class), 0));
                //排序
                query.orderBy(cb.asc(root.get("id").as(Integer.class)));
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }

        };
        return stationDao.findAll(spec, pageable);
    }

    /**
     * 请求日志:条件查询时动态组装条件+分页
     * @param socketLogSendSearchUtil
     * @return
     */
    public Page<SocketLogSend> getSocketLogSendAndDynamic(SocketLogSendSearchUtil socketLogSendSearchUtil, PageUtil pageUtil) {
        Pageable pageable=new PageRequest(pageUtil.getPageNumber()-1, pageUtil.getPageSize());  //分页信息
        //动态条件
        Specification<SocketLogSend> spec = new Specification<SocketLogSend>(){
            @Override
            public Predicate toPredicate(Root<SocketLogSend> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();

                //根据ID查询
                if (socketLogSendSearchUtil.getId()!=null) {
                    list.add(cb.equal(root.get("id").as(Integer.class), socketLogSendSearchUtil.getId()));
                }

                //根据数据类型查询
                if (socketLogSendSearchUtil.getType()!=null) {
                    list.add(cb.equal(root.get("type").as(Integer.class), socketLogSendSearchUtil.getType()));
                }

                //根据状态查询
                if (socketLogSendSearchUtil.getStatus()!=null) {
                    list.add(cb.equal(root.get("status").as(Integer.class), socketLogSendSearchUtil.getStatus()));
                }

                //模糊查询车牌号
                if (StringUtils.isNotEmpty(socketLogSendSearchUtil.getCarNumber())) {
                    list.add(cb.like(root.get("carNumber").as(String.class), "%" + socketLogSendSearchUtil.getCarNumber() + "%"));
                }

                //模糊查询发送类型
                if (StringUtils.isNotEmpty(socketLogSendSearchUtil.getServiceType())) {
                    list.add(cb.like(root.get("serviceType").as(String.class), "%" + socketLogSendSearchUtil.getServiceType() + "%"));
                }

                //模糊查询命令编码
                if (StringUtils.isNotEmpty(socketLogSendSearchUtil.getCode())) {
                    list.add(cb.like(root.get("code").as(String.class), "%" + socketLogSendSearchUtil.getCode() + "%"));
                }

                //比较开始日期
                if (StringUtils.isNotEmpty(socketLogSendSearchUtil.getStartDateTime())) {
                    list.add(cb.greaterThanOrEqualTo(root.get("createTime").as(Date.class), CoreDateUtils.parseDate(socketLogSendSearchUtil.getStartDateTime())));
                };
                //比较结束日期
                if (StringUtils.isNotEmpty(socketLogSendSearchUtil.getEndDateTime())) {
                    list.add(cb.lessThanOrEqualTo(root.get("createTime").as(Date.class), CoreDateUtils.parseDate(socketLogSendSearchUtil.getEndDateTime())));
                }
                //排序
                query.orderBy(cb.asc(root.get("id").as(Integer.class)));
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }

        };
        return socketLogSendDao.findAll(spec, pageable);
    }
}
